package com.yb.bussiness.shoper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgPingJia;
import com.yb.util.comm.DateTimeUtil;
import com.yb.util.framework.PageUtil;

public class TgPingJiaServiceImpl implements TgPingJiaService {
	private IBaseDao<TgPingJia, Long> dao;

	public TgPingJiaServiceImpl() {
		dao = new BaseDao<TgPingJia, Long>(TgPingJia.class);
	}

	/**
	 * 增加评价
	 */
	public boolean saveOrUpdateTgPingJia(TgPingJia tg) {
		try {
			dao.saveOrUpdate(tg);
			if(null==tg.getReply()&&null==tg.getReplyTime()){
				String sql = "update t_g_pingjia set reply = null,replyTime = null where id = ?";
				dao.update(sql, new Object[] { tg.getId() });
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 根据内容查询
	 */
	public List<TgPingJia> searchByParaMap(HashMap<String, String> paraMap) {
		String str = " SELECT  p.id AS id ,p.content AS content ,p.title AS title,t.name AS brandName, p.type as type  FROM t_g_pingjia p  INNER JOIN  t_g_brand t ON  p.brandId = t.id WHERE 1=1  ";
		if (paraMap.get("brandid") != null) {

			str += " and  p.brandId =" + paraMap.get("brandid");
		}
		if (paraMap.get("type") != null) {

			str += " and  p.type =" + paraMap.get("type");
		}
		if (paraMap.get("page") != null && paraMap.get("limit") != null) {
			int start = Integer.parseInt(paraMap.get("page")) - 1;
			int limit = Integer.parseInt(paraMap.get("limit"));
			str += " limit " + (start * limit) + "," + limit;
		}
		List<TgPingJia> list = dao.findList(str);
		return list;
	}

	public TgPingJia findById(String id) {
		TgPingJia tp = dao
				.uniqueResult(
						" SELECT p.id AS id ,p.createTime AS createTime ,p.content AS content ,p.title AS title ,p.type AS type, s.username AS username FROM  t_g_pingjia p LEFT JOIN t_s_sysuser s ON p.yeId =s.id  WHERE p.id=?  ",
						new Object[] { id });
		if (tp != null) {
			if (tp.getCreateTime() != null) {
				tp.setCreateTimeStr(DateTimeUtil.switchDateToString(tp
						.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}

		}
		return tp;
	}

	public int count(Map<String, String> parameMap) {
		try {
			String str = "  SELECT  * FROM t_g_pingjia  where 1=1 ";
			if (parameMap.get("brandid") != null) {

				str += " and  brandId =" + parameMap.get("brandid");
			}
			if (parameMap.get("type") != null) {

				str += " and  type =" + parameMap.get("type");
			}
			List<TgPingJia> list = dao.findList(str);
			if (list != null) {
				return list.size();
			}
		} catch (Exception e) {

			return 0;
		}
		return 0;
	}

	/**
	 * 根据业主评价记录查询
	 */
	public List<TgPingJia> searchRecord(Map<String, String> paraMap) {
		String str = " SELECT t.id as id ,t.title,s.name  AS brandName, t.content AS content ,DATE_FORMAT(t.createTime,'%Y-%c-%d %H:%i:%s') as createTimeStr FROM   t_g_pingjia t LEFT JOIN  t_g_brand  s ON  t.brandId = s.id where 1=1 ";
		if (paraMap.get("yeid") != null) {

			str += " and  yeId =" + paraMap.get("yeid");
		}
		str+=" order by t.createTime desc";
		if (paraMap.get("page") != null && paraMap.get("limit") != null) {
			int start = Integer.parseInt(paraMap.get("page")) - 1;
			int limit = Integer.parseInt(paraMap.get("limit"));
			str += " limit " + (start * limit) + "," + limit;
		}
		List<TgPingJia> list = dao.findList(str);
		return list;

	}

	/**
	 * 根据业主评价记录总数查询
	 */
	public int recordcount(Map<String, String> parameMap) {
		try {
			String str = "  SELECT s.name  AS title, t.content AS content FROM   t_g_pingjia t LEFT JOIN  t_g_brand  s ON  t.brandId = s.id where 1=1 ";
			if (parameMap.get("yeid") != null) {

				str += " and  yeId =" + parameMap.get("yeid");
			}
			List<TgPingJia> list = dao.findList(str);
			if (list != null) {
				return list.size();
			}
		} catch (Exception e) {

			return 0;
		}
		return 0;
	}

	public PageUtil findPjByBrandId(Long brandId,String flag, int page, int limit) {
		String sql = " select tgp.*,tss.username,mem.sex from t_g_pingjia tgp,t_s_sysuser tss,t_s_member mem where tgp.yeId = tss.id and tss.id=mem.sysuserId and tgp.brandId = ? and tgp.pjtype="+flag+" order by tgp.id desc limit ?,?";
		String countSql = "select count(*) from t_g_pingjia tgp,t_s_sysuser tss,t_s_member mem where tgp.yeId = tss.id and tss.id=mem.sysuserId and tgp.brandId = ? and tgp.pjtype="+flag+"";
		List<Object[]> countList = dao.findObjectArryList(countSql,
				new Object[] { brandId });
		int count = 0;
		if (null != countList && countList.size() > 0) {
			Object[] countAry = countList.get(0);
			count = new Integer(countAry[0].toString());
		}
		List<TgPingJia> list = new ArrayList<TgPingJia>();
		if (count > 0)
			list = dao.findList(sql, new Object[] { brandId,
					(page - 1) * limit, limit });
		PageUtil pu = new PageUtil();
		List<TgPingJia> alist = new ArrayList<TgPingJia>();
		for (TgPingJia pj : list) {
			TgPingJia bean = new TgPingJia();
			bean.setBrandId(pj.getBrandId());
			bean.setId(pj.getId());
			bean.setCreateTimeStr(DateTimeUtil.switchDateToString(pj
					.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			bean.setReplyTimeStr(DateTimeUtil.switchDateToString(pj
					.getReplyTime(), "yyyy-MM-dd HH:mm:ss"));
			bean.setYeId(pj.getYeId());
			bean.setUsername(pj.getUsername());
			bean.setSex(pj.getSex());
			bean.setType(pj.getType());
			bean.setTitle(pj.getTitle());
			bean.setContent(pj.getContent());
			bean.setReply(pj.getReply());
			alist.add(bean);
		}
		pu.setList(alist);
		pu.setTotalRecords(count);
		pu.setPageNo(page);
		pu.setPageSize(limit);
		return pu;
	}

	public Map<String, Object> findPj(TgPingJia pingJia, String page, String row) {
		String sql = "select tgp.*,tss.username from t_g_pingjia tgp,t_s_sysuser tss where tgp.yeId = tss.id and tgp.brandId= (select id from t_g_brand tgb where tgb.sysuserId = "
				+ +pingJia.getBrandId() + ") ";
		String countSql = "select count(1) from t_g_pingjia tgp,t_s_sysuser tss where tgp.yeId = tss.id and tgp.brandId=(select id from t_g_brand tgb where tgb.sysuserId = "
				+ +pingJia.getBrandId() + ") ";
		if (pingJia != null) {
			if ("1".equals(pingJia.getReply())) {
				sql += " and tgp.reply is not null ";
				countSql += " and tgp.reply is not null ";
			}
			if ("2".equals(pingJia.getReply())) {
				sql += " and tgp.reply is  null ";
				countSql += " and tgp.reply is  null ";
			}
			if (pingJia.getUsername() != null
					&& !"".equals(pingJia.getUsername())) {
				sql += " and tss.username like '%" + pingJia.getUsername()
						+ "%'";
				countSql += " and tss.username like '%" + pingJia.getUsername()
						+ "%'";
			}
			if (pingJia.getType() != null && !"".equals(pingJia.getType())) {
				sql += " and tgp.type = " + pingJia.getType();
				countSql += " and tgp.type = " + pingJia.getType();
			}
			if (pingJia.getStartTime() != null
					&& !"".equals(pingJia.getStartTime())) {
				sql += " and tgp.createTime >= '" + pingJia.getStartTime()
						+ " 00:00:00'";
				countSql += " and tgp.createTime >= '" + pingJia.getStartTime()
						+ " 00:00:00'";
			}
			if (pingJia.getEndTime() != null
					&& !"".equals(pingJia.getEndTime())) {
				sql += " and tgp.createTime <= '" + pingJia.getEndTime()
						+ " 23:59:59'";
				countSql += " and tgp.createTime <= '" + pingJia.getEndTime()
						+ " 23:59:59'";
			}
		}
		if (page != null && row != null) {
			int snum = Integer.parseInt(page) - 1;
			sql += " order by tgp.id desc   limit  "
					+ (snum * Integer.parseInt(row)) + "," + row + " ";
		}
		List<TgPingJia> list = dao.findList(sql);
		List<TgPingJia> alist = new ArrayList<TgPingJia>();
		for (TgPingJia pj : list) {
			pj.setCreateTimeStr(DateTimeUtil.switchDateToString(pj
					.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			pj.setReplyTimeStr(DateTimeUtil.switchDateToString(pj
					.getReplyTime(), "yyyy-MM-dd HH:mm:ss"));
//			TgPingJia bean = new TgPingJia();
//			bean.setBrandId(pj.getBrandId());
//			bean.setId(pj.getId());
//			bean.setCreateTimeStr(DateTimeUtil.switchDateToString(pj
//					.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
//			bean.setReplyTimeStr(DateTimeUtil.switchDateToString(pj
//					.getReplyTime(), "yyyy-MM-dd HH:mm:ss"));
//			bean.setYeId(pj.getYeId());
//			bean.setUsername(pj.getUsername());
//			bean.setType(pj.getType());
//			bean.setType(pj.getType());
//			bean.setTitle(pj.getTitle());
//			bean.setContent(pj.getContent());
//			bean.setReply(pj.getReply());
			alist.add(pj);
		}

		int total = 0;
		if (list.size() != 0) {
			total = Integer
					.parseInt((dao.findObjectArryList(countSql).get(0))[0] + "");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", alist);
		map.put("total", total);
		return map;
	}

	public TgPingJia read(Long id) {
		return dao.read(id);
	}

	public void saveReply(Long id, String replyContent) {
		String sql = "update t_g_pingjia set reply = ?,replyTime = now() where id = ?";
		dao.update(sql, new Object[] { replyContent, id });
	}

	public void deleteByIdString(String id) {
		String sql = "delete from t_g_pingjia where id = ?";
		dao.update(sql, new Object[] { id });
	}

	public int isPingjia(Long brandId, Long userId) {
		String sql = "select count(*) from t_g_pingjia where yeId=? and brandId=?";
		List list = dao.findObjectArryList(sql, new Object[]{userId,brandId});
		Object[] obj = (Object[])list.get(0);
		return Integer.valueOf(obj[0].toString());
	}
}
